package com.ssy.lingxi.common.constant.order;

import com.ssy.lingxi.common.language.LanguageHolder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 订单支付方式枚举
 * @author 万宁
 * @version 2.0.0
 * @date 2021-07-19
 */
public enum OrderPayTypeEnum {
    /**
     * 无需支付 - 0
     */
    DOES_NOT_NEED(0, "无需支付", Stream.of(OrderPayChannelEnum.DOES_NOT_NEED).collect(Collectors.toList()), false),

    /**
     * 线上支付 - 1
     */
    ONLINE_PAYMENT(1, "线上支付", Stream.of(OrderPayChannelEnum.ALIPAY, OrderPayChannelEnum.WECHAT_PAY, OrderPayChannelEnum.UNION_PAY, OrderPayChannelEnum.ACCOUNT_BALANCE).collect(Collectors.toList()), true),

    /**
     * 线下支付 - 2
     */
    OFFLINE_PAYMENT(2, "线下支付", Stream.of(OrderPayChannelEnum.OFFLINE).collect(Collectors.toList()), true),

    /**
     * 授信支付 - 3
     */
    CREDIT_LINE_PAYMENT(3, "授信额度支付", Stream.of(OrderPayChannelEnum.CREDIT).collect(Collectors.toList()), true),

    /**
     * 货到付款 - 4
     */
    CASH_ON_DELIVERY(4, "货到付款", Stream.of(OrderPayChannelEnum.COLLECT_ON_DELIVERY).collect(Collectors.toList()), true),

    /**
     * 结算支付 - 5
     */
    SETTLEMENT(5, "结算支付", Stream.of(OrderPayChannelEnum.MONTHLY_SETTLEMENT, OrderPayChannelEnum.ACCOUNT_PERIOD).collect(Collectors.toList()), false),

    /**
     * 通联支付 - 6
     */
    ALLIN_PAY(6, "通联支付", Stream.of(OrderPayChannelEnum.ALLIN_WECHAT, OrderPayChannelEnum.ALLIN_ALI_PAY, OrderPayChannelEnum.ALLIN_QUICK, OrderPayChannelEnum.ALLIN_UNION, OrderPayChannelEnum.ALLIN_BALANCE).collect(Collectors.toList()), true),

    /**
     * 建行支付 - 7
     */
    CCB_PAY(7, "建行支付", Stream.of(OrderPayChannelEnum.CCB_B2B, OrderPayChannelEnum.CCB_DIGITAL).collect(Collectors.toList()), true),

    /**
     * 跨境电商进口支付 - 8
     */
    COMMERCE_IMPORT_PAY(8, "跨境电商进口支付", Stream.of(OrderPayChannelEnum.COMMERCE_IMPORT).collect(Collectors.toList()), true),

    /**
     * 积分支付 - 10
     */
    MEMBER_RIGHT(10, "积分支付", Stream.of(OrderPayChannelEnum.RIGHT_POINT).collect(Collectors.toList()), false);

    OrderPayTypeEnum(Integer code, String name, List<OrderPayChannelEnum> payChannels, Boolean useForPaymentSetting) {
        this.code = code;
        this.payChannels = payChannels;
        this.useForPaymentSetting = useForPaymentSetting;
    }

    /**
     * 支付方式枚举值
     */
    private Integer code;

    /**
     * 支付方式对应的支付渠道列表
     */
    private List<OrderPayChannelEnum> payChannels;

    /**
     * 是否用于会员支付策略配置
     */
    private Boolean useForPaymentSetting;

    public Integer getCode() {
        return code;
    }

    public String getName() {
        return LanguageHolder.getName(this.getClass(), this.getCode());
    }

    public List<OrderPayChannelEnum> getPayChannels() {
        return payChannels;
    }

    public Boolean getUseForPaymentSetting() {
        return useForPaymentSetting;
    }

    /**
     * 根据枚举值获得枚举
     * @param code 枚举值
     * @return 枚举类型
     */
    public static OrderPayTypeEnum parse(Integer code) {
        return Arrays.stream(OrderPayTypeEnum.values()).filter(e -> e.getCode().equals(code)).findFirst().orElse(null);
    }

    /**
     * 根据枚举值获得支付方式名称
     * @param code 枚举值
     * @return 支付方式名称
     */
    public static String getNameByCode(Integer code) {
        return LanguageHolder.getName(OrderPayTypeEnum.class, code);
    }

    /**
     * 根据枚举值，获得支付方式对应的支付渠道列表
     * @param code 枚举值
     * @return 支付渠道列表
     */
    public static List<OrderPayChannelEnum> getChannelsByCode(Integer code) {
        OrderPayTypeEnum typeEnum = Arrays.stream(OrderPayTypeEnum.values()).filter(e -> e.getCode().equals(code)).findFirst().orElse(null);
        return typeEnum == null ? new ArrayList<>() : typeEnum.getPayChannels();
    }
}
